//-----------------------------------------------------------------------
// <copyright file="CurrencyDAOBase.cs" company="Pyramid Consulting">
//     Copyright (c) Pyramid Consulting. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Collections;
using MyFramework;
using MyFramework.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using MIMS.WRS.Entities;

namespace MIMS.WRS.Data
{    
    public abstract class CurrencyDAOBase
    {
        #region Common methods
        public virtual Currency CreateCurrencyFromReader(IDataReader reader)
        {
            Currency item = new Currency();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("CurrencyID"))) item.CurrencyID = (int)reader["CurrencyID"];
                if (!reader.IsDBNull(reader.GetOrdinal("Code"))) item.Code = (string)reader["Code"];
                if (!reader.IsDBNull(reader.GetOrdinal("Description"))) item.Description = (string)reader["Description"];
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateCurrencyFromReaderException, ex);
            }
            return item;
        }
        #endregion
        
        #region CreateCurrency methods
            
        public virtual void CreateCurrency(Currency currency)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spCurrencyInsert");
                
                dbCommandWrapper.AddInParameter("@Code", DbType.AnsiString, currency.Code);
                dbCommandWrapper.AddInParameter("@Description", DbType.AnsiString, currency.Description);
                dbCommandWrapper.AddOutParameter("@CurrencyID", DbType.Int32, 0);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommandWrapper);
                currency.CurrencyID = (int)dbCommandWrapper.GetParameterValue("@CurrencyID");
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateCurrencyException, ex);
            }
        }

        #endregion

        #region UpdateCurrency methods
        
        public virtual void UpdateCurrency(Currency currency)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spCurrencyUpdate");            
                
                dbCommandWrapper.AddInParameter("@CurrencyID", DbType.Int32, currency.CurrencyID);
                dbCommandWrapper.AddInParameter("@Code", DbType.AnsiString, currency.Code);
                dbCommandWrapper.AddInParameter("@Description", DbType.AnsiString, currency.Description);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommandWrapper);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessUpdateCurrencyException, ex);
            }
        }
        
        #endregion

        #region DeleteCurrency methods
        public virtual void DeleteCurrency(int currencyID)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spCurrencyDelete");
                
                dbCommandWrapper.AddInParameter("@CurrencyID", DbType.Int32, currencyID);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommandWrapper);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessDeleteCurrencyException, ex);
            }
        }

        #endregion

        #region GetCurrency methods
        
        public virtual Currency GetCurrency(int currencyID)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spCurrencyGet");
                
                dbCommandWrapper.AddInParameter("@CurrencyID", DbType.Int32, currencyID);
                
                Currency currency =  null;
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommandWrapper))
                {
                    if (reader.Read())
                    {
                        currency = CreateCurrencyFromReader(reader);
                        reader.Close();
                    }
                }
                return currency;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetCurrencyException, ex);
            }
        }
        
        #endregion

        #region GetCurrencies methods
        public virtual CurrencyCollection GetCurrencies(CurrencyColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DBCommandWrapper dbCommandWrapper = database.GetStoredProcCommandWrapper("spCurrencyGetList");
                
                dbCommandWrapper.AddInParameter("@OrderBy", DbType.AnsiString, orderBy.ToString());
                dbCommandWrapper.AddInParameter("@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                dbCommandWrapper.AddInParameter("@Page", DbType.Int32, page);
                dbCommandWrapper.AddInParameter("@PageSize", DbType.Int32, pageSize);
                dbCommandWrapper.AddOutParameter("@TotalRecords", DbType.Int32, 4);
                
                CurrencyCollection currencyCollection = new CurrencyCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommandWrapper))
                {
                    while (reader.Read())
                    {
                        Currency currency = CreateCurrencyFromReader(reader);
                        currencyCollection.Add(currency);
                    }
                    reader.Close();
                }
                totalRecords = (int)dbCommandWrapper.GetParameterValue("@TotalRecords");
                return currencyCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetCurrenciesException, ex);
            }
        }
        
        public virtual CurrencyCollection GetCurrencies(CurrencyColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return GetCurrencies(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
    }
}